//
//  AgreementViewController.m
//  customer
//
//  Created by Holothurian on 2018/10/20.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "AgreementViewController.h"

static NSString * const AgreementTableViewCellID = @"AgreementTableViewCellID";
@interface AgreementViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation AgreementViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadSource];
    [self layoutUI];
}

- (void)loadSource{
    self.view.backgroundColor = [UIColor clearColor];
    self.dataSource = [NSMutableArray arrayWithArray:@[@"欢迎您使用服务！",@"请务必在使用本协议()前认真阅读和理解《用户服务协议》(以下简称“本协议”)，特别是免除或者限制公司责任的免责条款及对用户的权利限制条款。您接受本协议条款，否则您将无权下载、安装或使用（以下简称“本软件”）及其相关服务。您的下载、安装或其他使用行为，将视为对本协议的接受，并同意接受各项条款的约束。",@"本协议是您与公司之间关于您下载、安装、使用、复制本软件，以及使用公司相关服务所订立的协议。“用户”或“您”是指通过公司提供的获取软件授权的途径获得软件授权许可和/或软件产品的个人或单一实体。",@"公司有权随时根据有关法律法规的变化、经营状况、经营策略调整和软件升级等原因修订本协议。"]];
}

- (void)layoutUI{
    
    AutoWeakSelf
    UIView * bgView = [UIView new];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 5;
    bgView.layer.borderColor = RGBColor(151, 151, 151).CGColor;
    bgView.layer.borderWidth = 1.0f;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view.mas_left).offset(15);
        make.right.equalTo(weakSelf.view.mas_right).offset(-15);
        make.centerY.equalTo(weakSelf.view.mas_centerY);
        make.height.mas_equalTo(420);
    }];
    
    UILabel * titleLabel = [UILabel new];
    titleLabel.text = @"用户服务协议";
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(bgView);
        make.height.mas_equalTo(60);
    }];
    
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(28, 60, F_DEVICE_W -30-56, 420-60-66) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [bgView addSubview:tableView];
    tableView.estimatedRowHeight = 50;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:AgreementTableViewCellID];
    
    UIView * lineView = [UIView new];
    lineView.backgroundColor = RGBColor(228, 228, 228);
    [bgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView.mas_centerX);
        make.left.right.equalTo(bgView);
        make.top.equalTo(tableView.mas_bottom).offset(5);
        make.height.mas_equalTo(1);
    }];
    
    UIView * lineView1 = [UIView new];
    lineView1.backgroundColor = RGBColor(228, 228, 228);
    [bgView addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView.mas_centerX);
        make.width.mas_equalTo(1);
        make.top.equalTo(lineView.mas_bottom);
        make.bottom.equalTo(bgView.mas_bottom).offset(-5);
    }];
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [leftButton setTitle:@"拒绝" forState:UIControlStateNormal];
    [leftButton setTitleColor:RGBColor(213, 26, 40) forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(bgView);
        make.top.equalTo(lineView.mas_bottom);
        make.right.equalTo(lineView1.mas_left);
    }];
    
    UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [rightButton setTitle:@"同意" forState:UIControlStateNormal];
    [rightButton setTintColor:RGBColor(9, 187, 7)];
    [rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(bgView);
        make.top.equalTo(lineView.mas_bottom);
        make.left.equalTo(lineView1.mas_right);
    }];
    
}

- (void)leftButtonClick:(UIButton *)button{
    [self showTipWithTitle:@"程序即将退出！"];
    [self performSelector:@selector(logoutVC) withObject:nil afterDelay:1.0f];
}

- (void)logoutVC{
    exit(0);
}

- (void)rightButtonClick:(UIButton *)button{
    K_UserDefault_Write(@"是", Customer_Agreement);
    [self showTipWithTitle:@"欢迎您使用本服务！"];
    [self performSelector:@selector(disVC) withObject:nil afterDelay:1.0f];
}

- (void)disVC{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:AgreementTableViewCellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.textColor = [UIColor darkTextColor];
    cell.textLabel.text = self.dataSource[indexPath.row];
    
    return cell;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
